define([ './module' ], function(sysrolemodule) {
	'use strict';





	//系统模块权限功能
	sysrolemodule.controller('sysmoduleaction',serverSysModuleActionCtrl);
	function serverSysModuleActionCtrl($state,$scope,$http,$location) {
		$scope.gtparams = [];
		$scope.index = 1;
		$scope.addgtparams = function () {
			var param = {actionurlid: ++$scope.index, id: '0', type: '1', sysmoduleactionname: '', actionurl: '',resourcetype:'',permission:''};
			$scope.gtparams.push(param);
		}
		var operate = $location.search().operate;
		if (operate != undefined && operate == "edit") {

			//控制保存按钮显示
			$scope.updatebtn = {show: true};
			$scope.sysmodulename=$location.search().sysmodulename;
			$scope.title = '正在对--' + $scope.sysmodulename+ '--权限进行编辑';
			var sysmoduleid = $location.search().sysmoduleid;
			if (sysmoduleid != undefined && sysmoduleid != "") {
				$http({
					method: 'POST',
					url: '../mall/sysmoduleaction/findAllBySysModuleId',
					data: {
						'sysmoduleid':sysmoduleid
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag) {
						$scope.gtparams = data.beanlist;
						$scope.updatebtn = {show: true};
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});




				$scope.update = function () {
					var actionurl = angular.toJson($scope.gtparams);
					var sysmodulename = $location.search().sysmodulename;
					var sysmoduleid = $location.search().sysmoduleid;
					$http({
						method: 'POST',
						url: '../mall/sysmoduleaction/updateMulitAction',
						data: {
							'sysmodulename': sysmodulename,
							'sysmoduleid': sysmoduleid,
							'actionurl': actionurl
						}
					}).success(function (data, status, headers, config) {
						if (data.sucflag) {
							$scope.info = {
								show: true,
								msg: '模块权限更新成功'
							}
							$location.path('/sysmodulement');
						}
					}).error(function (data, status, headers, config) {
						$scope.errors = {
							show: true,
							msg: '系统异常'
						}
					});

				}

			}
		}
	}





	//系统模块权限控制器
	sysrolemodule.controller('sysmoduleactionsingle',serverSysModuleActionSingleCtrl);
	function serverSysModuleActionSingleCtrl($state,$scope,$http,$location){
		//通过location中的operate参数区分操作行为
		var operate=$location.search().operate;
		//如果operate是edit则执行保存行为
		if(operate=='edit'){
			var id=$location.search().id;
			if(id!=undefined&&id!=""){
				$http({
					method:'POST',
					url:'../mall/sysmoduleaction/find',
					data:{
						'id':id
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.title='正在对--'+data.bean.sysmodulename+'--权限进行编辑';
						$scope.sysmoduleactionname=data.bean.sysmoduleactionname;
						$scope.actionurl=data.bean.actionurl;
						$scope.sysmoduleid=data.bean.sysmoduleid;
						$scope.sysmodulename=data.bean.sysmodulename;
						$scope.permission=data.bean.permission;
						$scope.resourcetype=data.bean.resourcetype;
						$scope.type=data.bean.type;
						$scope.status=data.bean.status;
						$scope.updatebtn={show:true};
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});
				//更新数据方法
				$scope.update=function(){
					var sysmoduleactionname=$scope.sysmoduleactionname;
					if(sysmoduleactionname==undefined){
						$scope.errors={
							show:true,
							msg:'请输入权限规则名称'
						}
						return false;
					}
					var actionurl=$scope.actionurl;
					if(actionurl==undefined){
						$scope.errors={
							show:true,
							msg:'请输入拦截路径'
						}
						return false;
					}
					var resourcetype=$scope.resourcetype;
					if(resourcetype==undefined){
						$scope.errors={
							show:true,
							msg:'请输入页面元素'
						}
						return false;
					}
					var permission=$scope.permission;
					if(permission==undefined){
						$scope.errors={
							show:true,
							msg:'请输入权限规则'
						}
						return false;
					}
					$http({
						method:'POST',
						url:'../mall/sysmoduleaction/update',
						data:{
							'sysmodulename':$scope.sysmodulename,
							'sysmoduleid':$scope.sysmoduleid,
							'sysmoduleactionname':sysmoduleactionname,
							'actionurl':actionurl,
							'resourcetype':resourcetype,
							'permission':permission,
							'type':$scope.type,
							'status':$scope.status,
							'id':id
						}
					}).
					success(function(data,status,headers,config){
						if(data.sucflag){
							$scope.info={
								show:true,
								msg:'模块功能更新成功'
							}
							$location.path('/sysmoduleactionment');
						}
					}).
					error(function(data,status,headers,config){
						$scope.errors={
							show:true,
							msg:'系统异常'
						}
					});
				}

			}


		}


	}


	//系统模块查询
	sysrolemodule.controller('sysmoduleactionment',serverSysModuleActionMentCtrl);
	//查询列表数据
	function serverSysModuleActionMentCtrl($state,$http,$location,$compile,$scope,$rootScope,$resource,DTOptionsBuilder,DTColumnBuilder,DTAjaxRenderer){





		$scope.title="系统模块权限列表";
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="showCase.selectAll" ng-click="showCase.toggleAll(showCase.selectAll,showCase.selected)">';
		vm.dtOptions=DTOptionsBuilder.newOptions().withOption('ajax',{
				type:'POST',
				url:'../mall/sysmoduleaction/findByPage',
				dataSrc:'data'
			})
			.withOption('processing',false)
			.withOption('paging',true)
			.withOption('serverSide',true)
			.withOption('createdRow',function(row,data,dataIndex){
				$compile(angular.element(row).contents())($scope);
			})
			.withOption('headerCallback', function(header) {
				if (!$scope.headerCompiled) {
					$scope.headerCompiled = true;
					$compile(angular.element(header).contents())($scope);
				}
			})
			.withOption('stateSave', true)
			.withOption('rowCallback',rowCallback)
			.withPaginationType('full_numbers')
			.withLanguageSource('./app/language/chinese.json');
		$scope.$on('handleRequest',function(){
		});
		vm.dtColumns=[
			DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
				vm.selected[full.id]=false;
				return '<input type="checkbox" ng-model="showCase.selected['+data.id+']" ng-click="showCase.toggleOne(showCase.selected)">';
			}),
			DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			DTColumnBuilder.newColumn('sysmoduleactionname').withTitle('权限名称').notSortable(),
			DTColumnBuilder.newColumn('actionurl').withTitle('拦截路径').notSortable(),
			DTColumnBuilder.newColumn('resourcetype').withTitle('页面元素').notSortable(),
			DTColumnBuilder.newColumn('permission').withTitle('权限规则').notSortable(),

			DTColumnBuilder.newColumn('sysmodulename').withTitle('模块名称').notSortable(),
			DTColumnBuilder.newColumn('type').withTitle('类型').notSortable(),
			DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
			DTColumnBuilder.newColumn('version').withTitle('版本号').notSortable(),
			DTColumnBuilder.newColumn(null).withTitle('操作').notSortable().renderWith(actionHtml)
		];

		function actionHtml(data,type,full,meta){
			return "<button class='btn btn-warning' ng-click=\"edit('"+data.id+"','"+data.sysmodulename+"','"+data.sysmoduleid+"')\"><i class='fa fa-edit'></i></button>";
		}
		function reloadData(){
			var resetPaging = false;
			vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}

		/**
		 * 复杂搜索刷新数据
		 */
		function searchReloadData(){
			var resetPaging=false;
			var searchParams={
				isCustomSearch:1,
				name:''
			};
			if($scope.name!=undefined){
				searchParams.name=$scope.name;
				vm.dtOptions['ajax'].data=searchParams;
				vm.dtInstance.reloadData(callback, resetPaging);
			}
		}

		/**
		 * 表格
		 */
		$scope.searchdataTableCustom=function(){
			searchReloadData();
		}


		//表格中编辑按钮
		$scope.edit=function(id,sysmodulename,sysmoduleid){
			$location.path('/sysmoduleactionsingle').search({'operate':'edit','id':id,'sysmodulename':sysmodulename,'sysmoduleid':sysmoduleid});
		}


		/**
		 * 删除记录
		 */
		$scope.del=function(){
			var i=0;
			var ids=[];
			angular.forEach(vm.selected, function(data,index,array){
				if(data){
					i++;
					ids.push(index);
				}
			});
			if(i==0){
				$scope.errors={
					show:true,
					msg:'请选择一条记录'
				}
			}else{
				$scope.errors={
					show:false
				}
				//显示确认删除
				$('#delModal').modal('show');
				//批量删除数据
				var idstrs=ids.join(",");
				$http({
					method:'POST',
					url:'../mall/sysmoduleaction/del',
					data:{
						'id':idstrs
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
							show:true,
							msg:'删除成功'
						}
						reloadData();
					}

				}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});
			}
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;
		}

		function someClickHandler(info) {
			vm.message = info.id + ' - ' + info.name;
		}
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
			$('td', nRow).bind('click', function() {
				$scope.$apply(function() {
					vm.someClickHandler(aData);
				});
			});
			return nRow;
		}

	}



	//系统模块控制器
	sysrolemodule.controller('sysmodule',serverSysModuleCtrl);
	function serverSysModuleCtrl($state,$scope,$http,$location){
		//通过location中的operate参数区分操作行为
		var operate=$location.search().operate;
		if(operate!=undefined&&operate=="save"){
			//控制保存按钮显示
			$scope.savebtn={show:true};
			$scope.title='添加系统模块';
			$scope.status="1";

			//保存数据方法
			$scope.save=function(){
				var sysmodulename=$scope.sysmodulename;
				if(sysmodulename==undefined){
					$scope.errors={
						show:true,
						msg:'请输入模块名称'
					}
					return false;
				}
				var note=$scope.note;
				if(note==undefined){
					$scope.errors={
						show:true,
						msg:'请输入模块说明'
					}
					return false;
				}
				$http({
					method:'POST',
					url:'../mall/sysmodule/save',
					data:{
						'sysmodulename':sysmodulename,
						'status':$scope.status,
						'note':note
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
								show:true,
								msg:'模块添加成功'
						}
						$location.path('/sysmodulement');
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});

			}

		}
		//如果operate是edit则执行保存行为
		if(operate=='edit'){
			var id=$location.search().id;
			if(id!=undefined&&id!=""){
				$http({
					method:'POST',
					url:'../mall/sysmodule/find',
					data:{
						'id':id
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.title='正在对--'+data.bean.sysmodulename+'--进行编辑';
						$scope.sysmodulename=data.bean.sysmodulename;
						$scope.status=data.bean.status;
						$scope.note=data.bean.note;
						$scope.updatebtn={show:true};
					}
				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});
				//更新数据方法
				$scope.update=function(){
					var sysmodulename=$scope.sysmodulename;
					if(sysmodulename==undefined){
						$scope.errors={
							show:true,
							msg:'请输入模块名称'
						}
						return false;
					}
					var note=$scope.note;
					if(note==undefined){
						$scope.errors={
							show:true,
							msg:'请输入模块说明'
						}
						return false;
					}
					$http({
						method:'POST',
						url:'../mall/sysmodule/update',
						data:{
							'sysmodulename':sysmodulename,
							'status':$scope.status,
							'note':note,
							'id':id
						}
					}).
					success(function(data,status,headers,config){
						if(data.sucflag){
							$scope.info={
								show:true,
								msg:'模块更新成功'
							}
							$location.path('/sysmodulement');
						}
					}).
					error(function(data,status,headers,config){
						$scope.errors={
							show:true,
							msg:'系统异常'
						}
					});
				}

			}


		}
		$scope.remove=function(index){
			$scope.gtparams.splice(index,1);
		}

	}

	//系统模块查询
	sysrolemodule.controller('sysmodulement',serverSysModuleMentCtrl);
	//查询列表数据
	function serverSysModuleMentCtrl($state,$http,$location,$compile,$scope,$rootScope,$resource,DTOptionsBuilder,DTColumnBuilder,DTAjaxRenderer){
		$scope.title="系统模块列表";
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="showCase.selectAll" ng-click="showCase.toggleAll(showCase.selectAll,showCase.selected)">';
		vm.dtOptions=DTOptionsBuilder.newOptions().withOption('ajax',{
			type:'POST',
			url:'../mall/sysmodule/findByPage',
			dataSrc:'data'
		})
		.withOption('processing',false)
		.withOption('paging',true)
		.withOption('serverSide',true)
		.withOption('createdRow',function(row,data,dataIndex){
			$compile(angular.element(row).contents())($scope);
		})
		.withOption('headerCallback', function(header) {
      if (!$scope.headerCompiled) {
          $scope.headerCompiled = true;
          $compile(angular.element(header).contents())($scope);
      }
    })
    .withOption('stateSave', true)
    .withOption('rowCallback',rowCallback)
		.withPaginationType('full_numbers')
		.withLanguageSource('./app/language/chinese.json');
		$scope.$on('handleRequest',function(){
		});
		vm.dtColumns=[
				  DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
					  vm.selected[full.id]=false;
					  return '<input type="checkbox" ng-model="showCase.selected['+data.id+']" ng-click="showCase.toggleOne(showCase.selected)">';
				  }),
				  DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
				  DTColumnBuilder.newColumn('sysmodulename').withTitle('模块名称').notSortable(),
			      DTColumnBuilder.newColumn('note').withTitle('说明').notSortable(),
				  DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
				  DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
				  DTColumnBuilder.newColumn('version').withTitle('版本号').notSortable(),
				  DTColumnBuilder.newColumn(null).withTitle('操作').notSortable().renderWith(actionHtml)
		];

		function actionHtml(data,type,full,meta){
			return "<button class='btn btn-warning' ng-click=\"edit('"+data.id+"')\"><i class='fa fa-edit'></i></button>"+
				"<button sytle='margin-left:10px' class='btn btn-success' ng-click=\"sysmoduleaction('"+data.id+"','"+data.sysmodulename+"')\">模块权限</button>";
		}
		function reloadData(){
			var resetPaging = false;
	        vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}

		/**
		 * 复杂搜索刷新数据
		 */
		function searchReloadData(){
			var resetPaging=false;
			var searchParams={
				isCustomSearch:1,
				name:''
			};
			if($scope.name!=undefined){
				searchParams.name=$scope.name;
				vm.dtOptions['ajax'].data=searchParams;
				vm.dtInstance.reloadData(callback, resetPaging);
			}
		}

		/**
		 * 表格
		 */
		$scope.searchdataTableCustom=function(){
			searchReloadData();
		}

		/**
		 * 系统模块权限
		 */
		$scope.sysmoduleaction=function(id,name){
			$location.path('/sysmoduleaction').search({'operate':'edit','sysmoduleid':id,'sysmodulename':name});
		}

		//表格中编辑按钮
		$scope.edit=function(id,name){
			$location.path('/sysmodule').search({'operate':'edit','id':id});
		}

		/**
		 * 跳转到添加商品类型和参数页面
		 */
		$scope.save=function(){

			$location.path('/sysmodule').search({'operate':'save'});
		}

		/**
		 * 删除记录
		 */
		$scope.del=function(){
			var i=0;
			var ids=[];
			angular.forEach(vm.selected, function(data,index,array){
				if(data){
					i++;
					ids.push(index);
				}
			});
			if(i==0){
				$scope.errors={
						show:true,
						msg:'请选择一条记录'
				}
			}else{
				$scope.errors={
						show:false
				}
				//批量删除数据
				var idstrs=ids.join(",");
				$http({
					method:'POST',
					url:'../mall/sysmodule/del',
					data:{
						'id':idstrs
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
								show:true,
								msg:'删除成功'
						}
						reloadData();
					}

				}).
				error(function(data,status,headers,config){
					$scope.errors={
							show:true,
							msg:'系统异常'
					}
				});
			}
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;
		}

		function someClickHandler(info) {
	        vm.message = info.id + ' - ' + info.name;
	    }
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
	        $('td', nRow).bind('click', function() {
	            $scope.$apply(function() {
	                vm.someClickHandler(aData);
	            });
	        });
	        return nRow;
		}

	}
});
